package com.ilxqx.framework.system.config;

import com.qiniu.common.Zone;
import com.qiniu.storage.BucketManager;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 七牛云bean配置
 * @author venus
 */
@Configuration
public class QiNiuBeanConfig {

	private final QiNiuConfigProperties qiNiuConfigProperties;

	@Autowired
	public QiNiuBeanConfig(QiNiuConfigProperties qiNiuConfigProperties) {
		this.qiNiuConfigProperties = qiNiuConfigProperties;
	}

	/**
	 * 七牛云Auth对象
	 * @return Auth对象
	 */
	@Bean
	public Auth auth() {
		return Auth.create(this.qiNiuConfigProperties.getAccessKey(), this.qiNiuConfigProperties.getSecretKey());
	}

	/**
	 * 资源管理对象Bean
	 * @return 资源管理对象
	 */
	@Bean
	public BucketManager bucketManager() {
		Zone zone = this.getZone();
		return new BucketManager(this.auth(), new com.qiniu.storage.Configuration(zone));
	}

	/**
	 * 根据地区获取Zone对象
	 * @return Zone对象
	 */
	private Zone getZone() {
		String zone = this.qiNiuConfigProperties.getZone();
		switch (zone) {
			case "zone0":
				return Zone.zone0();
			case "zone1":
				return Zone.zone1();
			case "zone2":
				return Zone.zone2();
			case "zoneNa0":
				return Zone.zoneNa0();
			case "zoneAs0":
				return Zone.zoneAs0();
			default:
				return Zone.autoZone();
		}
	}

}
